package com.example.demo.materialPurchase.service.impl;

import com.example.demo.common.ResponseData;
import com.example.demo.entity.MaterialInfo;
import com.example.demo.entryWarehouse.entity.BaseInformation;
import com.example.demo.entryWarehouse.mapper.BaseInformationMapper;
import com.example.demo.materialPurchase.entity.MaterialBaseInfo;
import com.example.demo.materialPurchase.entity.MaterialPurchase;
import com.example.demo.materialPurchase.entity.PageBaseInfo;
import com.example.demo.materialPurchase.entity.PageMaterialInfo;
import com.example.demo.materialPurchase.mapper.MaterialPurchaseMapper;
import com.example.demo.materialPurchase.service.MaterialPurchaseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lby
 * @since 2021-05-09
 */
@Service
public class MaterialPurchaseServiceImpl extends ServiceImpl<MaterialPurchaseMapper, MaterialPurchase> implements MaterialPurchaseService {

	@Autowired
	private MaterialPurchaseMapper materialmapper;
	
	@Autowired
	private BaseInformationMapper basemapper;
	
	/**
	 * 插入一条新的采购信息,并将计划状态设置为 计划中
	 */
	@Override
	public ResponseData insNewOne(String tableId, String materialName, String departName, int purchaseNum,
			String partUsed, String preStatus, String remark) {
		
		// 根据物料名查询物料信息,并将计划状态设置为  preStatus
		QueryWrapper<BaseInformation> wrapBase = new QueryWrapper<BaseInformation>();
		wrapBase.eq("name", materialName);
		BaseInformation base = new BaseInformation();
		
		try {
			base = basemapper.selectOne(wrapBase);
			materialmapper.insertOneNew(tableId, materialName, base.getCode(), departName, purchaseNum, partUsed, preStatus, remark);
		} catch (Exception e) {
			// TODO: handle exception
			return ResponseData.buildError(e.toString());
		}
		return ResponseData.buildOk("Insert Success!");
	}

	/**
	 * 根据参数 preStatus 对 采购物料信息 状态 进行更改
	 */
	@Override
	public ResponseData UpdateStatus(String[] ids, String preStatus) {
		
		for (String idString: ids) {
			try {
				// 根据id 修改采购计划状态
				materialmapper.UpdateStatus(preStatus, Integer.valueOf(idString));
			} catch (Exception e) {
				// TODO: handle exception
				return ResponseData.buildError(e.toString());
			}
		}
		return ResponseData.buildOk("Update Success!");
	}

	/**
	 * 查询所有物料基本信息
	 */
	@Override
	public ResponseData selMaterialBases(int currentPage, int pageSize) {
		
		// 设置查询条件
		QueryWrapper<BaseInformation> wrapper = new QueryWrapper<BaseInformation>();
		// 根据currentPage 和 PageSize 查询信息
		Page<BaseInformation> page = new Page<>(currentPage, pageSize);
		IPage<BaseInformation> materialPage = basemapper.selectPage(page, wrapper);
		
		// 将查询的信息 赋值给 实体类 PageBaseInfo 并返回
		PageBaseInfo results = new PageBaseInfo();
		// 获取所有数据
		results.setAllMaterials(materialPage.getRecords());
		// 获取总页数
		results.setPagecount(materialPage.getPages());
		// 获取总记录个数
		results.setTotal(materialPage.getTotal());
		
		return ResponseData.buildOk(results);
	}

	/**
	 * 根据参数 分页查询所有采购信息
	 */
	@Override
	public ResponseData selAllPurchase(String preStatus, String departName, int currentPage, int pageSize) {
		// TODO Auto-generated method stub
		
//		// 设置查询条件
		QueryWrapper<MaterialBaseInfo> wrapper = new QueryWrapper<MaterialBaseInfo>();
		wrapper.eq("purchase_status", preStatus);
		wrapper.eq("depart_name", departName);
		
		// 根据currentPage 和 PageSize 查询信息
		Page<MaterialBaseInfo> pagetemp = new Page<MaterialBaseInfo>(currentPage, pageSize);
		
		IPage<MaterialBaseInfo> iPage = materialmapper.selAllPurchase(pagetemp, wrapper);
		System.out.println("data：" + iPage.getTotal());
		
		// 将查询的信息 赋值给 实体类 PageMaterialInfo 并返回
		PageMaterialInfo results =  new PageMaterialInfo();
		
		// 获取所有数据
		results.setAllMaterials(iPage.getRecords());
		
		// 获取总页数
		results.setPagecount(iPage.getPages());
		
		// 获取总记录个数
		results.setTotal(iPage.getTotal());
		
		return ResponseData.buildOk(results);
	}

}
